package com.easy_helth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy_helth.common.CustomException;
import com.easy_helth.dto.SetmealDto;
import com.easy_helth.entity.Setmeal;
import com.easy_helth.entity.SetmealDish;
import com.easy_helth.mapper.SetmealMapper;
import com.easy_helth.service.SetmealDishService;
import com.easy_helth.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;
    /**
     * //    保存菜品与套餐之间信息
     * @param setmealDto
     */
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
//        保存菜品基本信息
        this.save(setmealDto);

//        保存菜品与套餐的关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        log.info("信息：{}",setmealDishes);
        setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }


    /**
     * 删除套餐顺便删除相关菜品
     * @param ids
     */
    @Transactional
    public void removeMealWithDish(List<Long> ids) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new  LambdaQueryWrapper();
//        select * from ... where id in (  ) and state = 1;
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

//        输出有多少条符合条件
        int count = (int) this.count(queryWrapper);

        if (count > 0){
            throw new CustomException("套餐正在售卖中，不可取消");
        }
//        如果可以删除
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(lambdaQueryWrapper);
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
//        跟新setmeal基本信息
        this.updateById(setmealDto);
//        跟新setmeal_dish
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

//        跟新
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getByIdWithDish(int id) {
            //查询套餐基本信息
            Setmeal setmeal = this.getById(id);
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);

            //查询套餐菜品信息
            LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> list = setmealDishService.list(queryWrapper);

            setmealDto.setSetmealDishes(list);
            return setmealDto;
        }

}
